package com.bop.dehong.widget.dialog;

import com.bop.dehong.R;
import com.bop.dehong.widget.dialog.effects.BaseEffects;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NiftyDialogBuilder extends Dialog implements DialogInterface {

	private static Context mContext;
	private volatile static NiftyDialogBuilder instance;
	private View mDialogView;

	private LinearLayout mLinearLayoutView;
	private RelativeLayout mRelativeLayoutView;
	private LinearLayout mLinearLayoutMsgView;
	private LinearLayout mLinearLayoutTopView;
	private FrameLayout mFrameLayoutCustomView;
	private View mDivider;
	private TextView mTitle;
	private TextView mMessage;
	private ImageView mIcon;
	private Button mBtn_sure;
	private Button mBtn_cancel;
	private Effectstype type = null;// 动画类型
	private int mDuration = -1;
	private boolean isCancelable = true;
	/******* 默认参数 *******/
	private final String defTextColor = "#FFFFFFFF";
	private final String defDividerColor = "#11000000";
	private final String defMsgColor = "#FFFFFFFF";
	private final String defDialogColor = "#FFE74C3C";

	public NiftyDialogBuilder(Context context) {
		super(context);
		init(context);
	}

	public NiftyDialogBuilder(Context context, int theme) {
		super(context, theme);
		init(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		WindowManager.LayoutParams params = getWindow().getAttributes();
//		params.height = ViewGroup.LayoutParams.MATCH_PARENT;
//		params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//		getWindow().setAttributes(
//				(android.view.WindowManager.LayoutParams) params);
	}

	public static NiftyDialogBuilder getInstance(Context context) {
		if (instance == null || !mContext.equals(context)) {
			synchronized (NiftyDialogBuilder.class) {
				if (instance == null || !mContext.equals(context)) {
					instance = new NiftyDialogBuilder(context,
							R.style.MyDialogStyle);
				}
			}
		}
		mContext = context;
		return instance;
	}

	private void init(Context context) {
		// TODO Auto-generated method stub
		mDialogView = View.inflate(context, R.layout.dialog_layout, null);
		mRelativeLayoutView = (RelativeLayout) mDialogView
				.findViewById(R.id.dialog);
		mLinearLayoutView = (LinearLayout) mDialogView
				.findViewById(R.id.parent_panel);
		mLinearLayoutTopView = (LinearLayout) mDialogView
				.findViewById(R.id.top_panel);
		mLinearLayoutMsgView = (LinearLayout) mDialogView
				.findViewById(R.id.content_panel);
		mFrameLayoutCustomView = (FrameLayout) mDialogView
				.findViewById(R.id.custom_panel);
		mTitle = (TextView) mDialogView.findViewById(R.id.title);
		mMessage = (TextView) mDialogView.findViewById(R.id.message);
		mIcon = (ImageView) mDialogView.findViewById(R.id.icon);
		mDivider = mDialogView.findViewById(R.id.divider);
		mBtn_sure = (Button) mDialogView.findViewById(R.id.btn_sure);
		mBtn_cancel = (Button) mDialogView.findViewById(R.id.btn_cancel);
		setContentView(mDialogView);
//		this.setOnShowListener(new OnShowListener() {
//
//			@Override
//			public void onShow(DialogInterface dialog) {
//				// TODO Auto-generated method stub
//				mLinearLayoutView.setVisibility(View.VISIBLE);
//				if (type == null) {
//					type = Effectstype.Slidetop;
//				}
//				start(type);
//			}
//		});
		mRelativeLayoutView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isCancelable)
					dismiss();
			}
		});
	}

	public void toDefault() {
		mTitle.setTextColor(Color.parseColor(defTextColor));
		mDivider.setBackgroundColor(Color.parseColor(defDividerColor));
		mMessage.setTextColor(Color.parseColor(defMsgColor));
		mLinearLayoutView.setBackgroundColor(Color.parseColor(defDialogColor));
	}

	/**
	 * 分割线颜色设置
	 * 
	 * @param colorString
	 * @return
	 */
	public NiftyDialogBuilder withDividerColor(String colorString) {
		mDivider.setBackgroundColor(Color.parseColor(colorString));
		return this;
	}

	public NiftyDialogBuilder withDividerColor(int color) {
		mDivider.setBackgroundColor(color);
		return this;
	}

	/**
	 * 头部文字设置
	 * 
	 * @param title
	 * @return
	 */
	public NiftyDialogBuilder withTitle(CharSequence title) {
		toggleView(mLinearLayoutTopView, title);
		mTitle.setText(title);
		return this;
	}

	/**
	 * 头部文字颜色设置
	 * 
	 * @param colorString
	 * @return
	 */
	public NiftyDialogBuilder withTitleColor(String colorString) {
		mTitle.setTextColor(Color.parseColor(colorString));
		return this;
	}

	public NiftyDialogBuilder withTitleColor(int color) {
		mTitle.setTextColor(color);
		return this;
	}

	/**
	 * 信息文字设置
	 * 
	 * @param textResId
	 * @return
	 */
	public NiftyDialogBuilder withMessage(int textResId) {
		toggleView(mLinearLayoutMsgView, textResId);
		mMessage.setText(textResId);
		return this;
	}

	public NiftyDialogBuilder withMessage(CharSequence msg) {
		toggleView(mLinearLayoutMsgView, msg);
		mMessage.setText(msg);
		return this;
	}

	/**
	 * 信息文字颜色设置
	 * 
	 * @param colorString
	 * @return
	 */
	public NiftyDialogBuilder withMessageColor(String colorString) {
		mMessage.setTextColor(Color.parseColor(colorString));
		return this;
	}

	public NiftyDialogBuilder withMessageColor(int color) {
		mMessage.setTextColor(color);
		return this;
	}

	/**
	 * 对话框背景颜色设置
	 * 
	 * @param colorString
	 * @return
	 */
	public NiftyDialogBuilder withDialogColor(String colorString) {
		mLinearLayoutView.getBackground().setColorFilter(
				ColorUtils.getColorFilter(Color.parseColor(colorString)));
		return this;
	}

	public NiftyDialogBuilder withDialogColor(int color) {
		mLinearLayoutView.getBackground().setColorFilter(
				ColorUtils.getColorFilter(color));
		return this;
	}

	/**
	 * icon设置
	 * 
	 * @param drawableResId
	 * @return
	 */
	public NiftyDialogBuilder withIcon(int drawableResId) {
		mIcon.setImageResource(drawableResId);
		return this;
	}

	public NiftyDialogBuilder withIcon(Drawable icon) {
		mIcon.setImageDrawable(icon);
		return this;
	}

	/**
	 * 动画时间设置
	 * 
	 * @param duration
	 * @return
	 */
	public NiftyDialogBuilder withDuration(int duration) {
		this.mDuration = duration;
		return this;
	}

	/**
	 * 动画风格类型选择设置
	 * 
	 * @param type
	 * @return
	 */
	public NiftyDialogBuilder withEffect(Effectstype type) {
		this.type = type;
		return this;
	}

	/**
	 * 按键背景图片设置,左右按键背景图片一样
	 * 
	 * @param resid
	 * @return
	 */
	public NiftyDialogBuilder withButtonDrawable(int resid) {
		mBtn_sure.setBackgroundResource(resid);
		mBtn_cancel.setBackgroundResource(resid);
		return this;
	}

	/**
	 * 左边按键背景图片设置
	 * 
	 * @param resid
	 * @return
	 */
	public NiftyDialogBuilder withButtonLeftDrawable(int resid) {
		mBtn_sure.setBackgroundResource(resid);
		return this;
	}

	/**
	 * 右边按键背景图片设置
	 * 
	 * @param resid
	 * @return
	 */
	public NiftyDialogBuilder withButtonRightDrawable(int resid) {
		mBtn_cancel.setBackgroundResource(resid);
		return this;
	}

	/**
	 * 左边按键文字设置
	 * 
	 * @param text
	 * @return
	 */
	public NiftyDialogBuilder withButtonLeftText(CharSequence text) {
		mBtn_sure.setVisibility(View.VISIBLE);
		mBtn_sure.setText(text);

		return this;
	}

	/**
	 * 右边按键文字设置
	 * 
	 * @param text
	 * @return
	 */
	public NiftyDialogBuilder withButtonRightText(CharSequence text) {
		mBtn_cancel.setVisibility(View.VISIBLE);
		mBtn_cancel.setText(text);
		return this;
	}

	/**
	 * 左边按键点击事件设置
	 * 
	 * @param click
	 * @return
	 */
	public NiftyDialogBuilder setButtonLeftClick(View.OnClickListener click) {
		mBtn_sure.setOnClickListener(click);
		return this;
	}

	/**
	 * 右边按键点击事件设置
	 * 
	 * @param click
	 * @return
	 */
	public NiftyDialogBuilder setButtonRightClick(View.OnClickListener click) {
		mBtn_cancel.setOnClickListener(click);
		return this;
	}

	/**
	 * 自定义位置设置,可以添加自己的布局
	 * 
	 * @param resId布局layout的id
	 * @param context上下文参数
	 * @return
	 */
	public NiftyDialogBuilder setCustomView(int resId, Context context) {
		View customView = View.inflate(context, resId, null);
		if (mFrameLayoutCustomView.getChildCount() > 0) {
			mFrameLayoutCustomView.removeAllViews();
		}
		mFrameLayoutCustomView.addView(customView);
		return this;
	}

	/**
	 * 自定义位置设置,可以添加自己的布局
	 * 
	 * @param view自定义的view
	 * @param context上下文参数
	 * @return
	 */
	public NiftyDialogBuilder setCustomView(View view, Context context) {
		if (mFrameLayoutCustomView.getChildCount() > 0) {
			mFrameLayoutCustomView.removeAllViews();
		}
		mFrameLayoutCustomView.addView(view);

		return this;
	}

	/**
	 * 对话框之外的点击,是否取消对话框
	 * 
	 * @param cancelable
	 *            :true是,false否
	 * @return
	 */
	public NiftyDialogBuilder isCancelableOnTouchOutside(boolean cancelable) {
		this.isCancelable = cancelable;
		this.setCanceledOnTouchOutside(cancelable);
		return this;
	}

	/**
	 * 是否屏蔽返回键设置
	 * 
	 * @param cancelable
	 *            :设置为false,按返回键不能退出。默认为true。
	 * @return
	 */
	public NiftyDialogBuilder isCancelable(boolean cancelable) {
		this.isCancelable = cancelable;
		this.setCancelable(cancelable);
		return this;
	}

	private void toggleView(View view, Object obj) {
		if (obj == null) {
			view.setVisibility(View.GONE);
		} else {
			view.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 开始动画
	 * 
	 * @param type动画类型
	 */
	private void start(Effectstype type) {
		BaseEffects animator = type.getAnimator();
		if (mDuration != -1) {
			animator.setDuration(Math.abs(mDuration));
		}
		animator.start(mRelativeLayoutView);
	}

	/**
	 * 显示对话框
	 */
	@Override
	public void show() {
		super.show();
	}

	/**
	 * 取消对话框
	 */
	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		super.cancel();
	}

	@Override
	public void dismiss() {
		super.dismiss();
		mBtn_sure.setVisibility(View.GONE);
		mBtn_cancel.setVisibility(View.GONE);
	}
}
